﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Threading;

namespace DevOnServer
{
    public class CDevOnServer
    {
        [STAThread]
        public static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new CConsole());
        }

        public void start()
        {
            if (!_load())
                return;

            while (_main()) ;
        }

        public void exit()
        {
        }

        private bool _load()
        {
            Thread.CurrentThread.CurrentCulture = new System.Globalization.CultureInfo("en-US");

            CConsole.WriteLine("Loading...");

            m_network.setWaitingTime(33);

            if (!m_database.init(m_game))
                return false;

            if (!m_auth.init(m_network, m_database))
            {
                CConsole.WriteLine("Can't start AuthServer...");
                return false;
            }

            if (!m_char.init(m_network, m_database))
            {
                CConsole.WriteLine("Can't start CharServer...");
                return false;
            }

            if (!m_game.init(m_network, m_database))
            {
                CConsole.WriteLine("Can't start GameServer...");
                return false;
            }

            GC.Collect();

            CConsole.WriteLine("Server is running !");

            return true;
        }

        private bool _main()
        {
            return m_network.run()
                && m_auth.run()
                && m_char.run()
                && m_game.run();
        }

        private CNetwork m_network = new CNetwork();

        private CDatabase m_database = new CDatabase();

        private CAuthServer m_auth = new CAuthServer();

        private CCharServer m_char = new CCharServer();

        private CGameServer m_game = new CGameServer();
    }
}